﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Xml;

namespace Nasa8x.Web.UI.Configuration
{
    public class WebConfigSectionHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            return new SiteConfiguration(section);
        }

    }

    public sealed class SiteConfiguration
    {

        private const string CONFIG_SECTION = "SiteSettings";

        private static Dictionary<string ,string> values;

       // public static XmlNode _section;


        public SiteConfiguration(XmlNode node)
        {
            values=new Dictionary<string,string>();

            XmlNodeList nodes = node.ChildNodes;

            
            foreach (XmlNode _node in nodes)
            {
               
                values.Add(_node.Name, _node.InnerText);
            }

           


        }

        public string this[string key]
        {
            get
            {


                //XmlNode node = _section.SelectSingleNode(key);
                //if (node != null)
                //    return node.InnerText;
                //return null;

                return values[key];


            }
        }


        private static readonly Object _lock = new Object();

        public static SiteConfiguration Instant
        {
            get
            {
                lock (_lock)
                {

                    var settings = (SiteConfiguration)ConfigurationManager.GetSection(CONFIG_SECTION);

                    if (settings == null)
                    {
                        if (System.Web.HttpContext.Current == null)
                            throw new ApplicationException("The main forum control is not design-time compatible due to it's complexity. Please load the individual page controls to modify the website.");
                        throw new ApplicationException("Failed to get configuration from Web.config.");
                    }
                    return settings;
                }

            }
        }

        //// Create singleton object of the settings
        //public static SiteConfiguration Instance
        //{
        //    get
        //    {

        //        // get the config settings


        //        settings = (SiteConfiguration)ConfigurationManager.GetSection(CONFIG_SECTION);
        //        if (settings == null)
        //        {
        //            if (System.Web.HttpContext.Current == null)
        //                throw new ApplicationException("The main forum control is not design-time compatible due to it's complexity. Please load the individual page controls to modify the forum.");
        //            throw new ApplicationException("Failed to get configuration from Web.config.");
        //        }



        //        return settings;
        //    }
        //}

    }
}